package sketch.specs;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.TypeDeclaration;

import randoop.experiments.WriteModelCheckerDriver;
import sketch.ast.ASTUtils;
import sketch.examples.TreeMap;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class SymbolicValueVisitorTest extends TestCase {
	
	public static Test suite() {
		return new TestSuite(SymbolicValueVisitorTest.class);
	}
	
	public void testFindSymbolicValue() {
		String filePath = "./testfiles/sketch/specs/SymbolicValueCode.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		SymbolicValueVisitor visitor = new SymbolicValueVisitor();
		unit.accept(visitor);
		//System.out.println(unit.toString());
		assertTrue(visitor.getNumOfReplace() == 1);
		
		String[] methodSigs = visitor.getSymbolicMethodSignatures();
		assertEquals(1, methodSigs.length);
		
		assertEquals("remove(sym)", methodSigs[0]);
	}
	
	public void testWriteJPFDriver() {
		String filePath = "./testfiles/sketch/specs/SymbolicValueCode.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		List<AbstractTypeDeclaration> abTypes = unit.types();
		
		TypeDeclaration type = (TypeDeclaration)abTypes.get(0);
		String[] methods = new String[]{"testTreeMap2"};
		
		WriteJPFSymbolicTestDriver.writeTestDriver(null, type, ".", methods, null, new String[]{});
	}
	
	public void testSimpleTreeMap() {
//		String filePath = "./tests/sketch/specs/treemapsimple.txt";
		String filePath = "./testfiles/sketch/specs/TreeMapSimple.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		List<AbstractTypeDeclaration> abTypes = unit.types();
		
		TypeDeclaration type = (TypeDeclaration)abTypes.get(0);
		String[] methods = new String[]{"testRemove"};
		
		WriteJPFSymbolicTestDriver.writeTestDriver(null, type, ".", methods, null, new String[]{});
	}
	
	public void testFetchingSymMethods() {
		String filePath = "./testfiles/sketch/specs/TreeMapSimple.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		SymbolicValueVisitor visitor = new SymbolicValueVisitor();
		unit.accept(visitor);
		//System.out.println(unit.toString());
		assertTrue(visitor.getNumOfReplace() == 7);
		
		String[] methodSigs = visitor.getSymbolicMethodSignatures();
		assertEquals(7, methodSigs.length);
		
		//assertEquals("remove(sym)", methodSigs[0]);
		for(String methodSig : methodSigs) {
			System.out.println(methodSig);
		}
	}
	
	public void testFetchingSymMethodByMethod() {
		String filePath = "./testfiles/sketch/specs/TreeMapSimple.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		SymbolicValueVisitor visitor = new SymbolicValueVisitor();
		unit.accept(visitor);
		//System.out.println(unit.toString());
		assertTrue(visitor.getNumOfReplace() == 7);
		
		String[] methodSigs = visitor.getSymbolicMethodSignatures();
		assertEquals(7, methodSigs.length);
		
		String[] removeSigs = visitor.getSymbolicMethodSignaturesForMethod("testRemove");
		assertEquals(1, removeSigs.length);
		
		String[] putSigs = visitor.getSymbolicMethodSignaturesForMethod("testPut");
		assertEquals(1, putSigs.length);
		
		String[] put2Sigs = visitor.getSymbolicMethodSignaturesForMethod("test2Puts");
		assertEquals(1, put2Sigs.length);
		
		String[] putRemoveSigs = visitor.getSymbolicMethodSignaturesForMethod("testPutandRemove");
		assertEquals(2, putRemoveSigs.length);
	}
}